import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [
//通用路由
  {
    path: '/error',
    name: 'Error',
    component: () => import('../components/Error.vue')
  },
// 客户路由
  {
    //首页
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta:{
      roles:['admin']
    }
  },
  {
    //商品页
    path: '/goods',
    name: 'Goods',
    component: () => import('../views/Goods.vue'),
    meta:{
      roles:['admin']
    }
  },
  {
    //店铺
    path: '/shopGoods',
    name: 'shopGoods',
    component: () => import('../views/shopGoods.vue'),
    meta:{
      roles:['admin']
    }
  },
  {
    //关于我们
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue'),
    meta:{
      roles:['admin']
    }
  },
  {
    //商品详情页
    path: '/goods/details',
    name: 'Details',
    component: () => import('../views/Details.vue'),
    meta:{
      roles:['admin']
    }
  },
  {
    //购物车页
    path: '/shoppingCart',
    name: 'ShoppingCart',
    component: () => import('../views/ShoppingCart.vue'),
    meta: {
      requireAuth: true, // 需要验证登录状态
      roles:['admin']
    }
  },
  {
    //收藏页
    path: '/collect',
    name: 'Collect',
    component: () => import('../views/Collect.vue'),
    meta: {
      requireAuth: true, // 需要验证登录状态
      roles:['admin']
    }
  },
  {
    //订单页
    path: '/order',
    name: 'Order',
    component: () => import('../views/Order.vue'),
    meta: {
      requireAuth: true, // 需要验证登录状态
      roles:['admin']
    }
  },
  {
    //确认订单页
    path: '/confirmOrder',
    name: 'ConfirmOrder',
    component: () => import('../views/ConfirmOrder.vue'),
    meta: {
      requireAuth: true, // 需要验证登录状态
      roles:['admin']
    }
  },
// 商家路由
  {
    path: '/seller',
    component: () => import('../views/seller/Manage.vue'),
    // redirect:"/home",
    //  子路由的设置
    children:[
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/seller/Home.vue')
      },
      {
        path: '/allShop',
        name: 'allShop',
        component: () => import('../views/seller/AllShop.vue')
      },
      {
        path: '/itemManager',
        name: 'itemManager',
        component: () => import('../views/seller/ItemManager.vue')
      },
      {
        path: '/orderManager',
        name: 'orderManager',
        component: () => import('../views/seller/OrderManager.vue')
      },
      {
        path: '/allItem',
        name: 'allItem',
        component: () => import('../views/seller/AllItem.vue')
      },

    ]
  },

]

const router = new Router({
  // base: '/dist',
  // mode: 'history',
  routes
})

/* 由于Vue-router在3.1之后把$router.push()方法改为了Promise。所以假如没有回调函数，错误信息就会交给全局的路由错误处理。
vue-router先报了一个Uncaught(in promise)的错误(因为push没加回调) ，然后再点击路由的时候才会触发NavigationDuplicated的错误(路由出现的错误，全局错误处理打印了出来)。*/
// 禁止全局路由错误处理打印
const originalPush = Router.prototype.push;
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

// const urole = 'user'
// const brole='business'
// router.beforeEach((to,from,next)=>{
//   if(to.meta.roles.includes(urole)){
//     next({path:"/"})	//前往用户页面
//   }
//   else if(to.meta.roles.includes(brole)){
//     next()  //前往商家页面
//   }
//   else{
//     next({path:"/error"})	//跳到404页面
//   }
// })

export default router
